package bTree.LeetCode;

import java.util.HashMap;

public class 罗马数字转整数 {
    public int romanToInt(String s) {
        int sum = 0;
        HashMap<Character, Integer> hashMap = new HashMap<>();
        hashMap.put('I', 1);
        hashMap.put('V', 5);
        hashMap.put('X', 10);
        hashMap.put('L', 50);
        hashMap.put('C', 100);
        hashMap.put('D', 500);
        hashMap.put('M', 1000);
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == 'I' && i != s.length() - 1 && s.charAt(i + 1) == 'V') {
                sum += 4;
                i++;
                continue;
            }
            if (c == 'I' && i != s.length() - 1 && s.charAt(i + 1) == 'X') {
                sum += 9;
                i++;
                continue;

            }
            if (c == 'X' && i != s.length() - 1 && s.charAt(i + 1) == 'L') {
                sum += 40;
                i++;
                continue;

            }
            if (c == 'X' && i != s.length() - 1 && s.charAt(i + 1) == 'C') {
                sum += 90;
                i++;
                continue;

            }
            if (c == 'C' && i != s.length() - 1 && s.charAt(i + 1) == 'D') {
                sum += 400;
                i++;
                continue;

            }
            if (c == 'C' && i != s.length() - 1 && s.charAt(i + 1) == 'M') {
                sum += 900;
                i++;
                continue;

            }

           sum += hashMap.get(c);
        }
        return sum;
    }
}
